package tree.test173;

import listbyorder.utils.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/17 10:56
 */
public class BSTIterator2 {

    List<Integer> res;
    int index;

    public BSTIterator2(TreeNode root) {
        res = new ArrayList<>();
        dfs(root);
        index = 0;
    }

    private void dfs(TreeNode root){
        if(root == null) return;
        dfs(root.left);
        res.add(root.val);
        dfs(root.right);
    }

    /** @return the next smallest number */
    public int next() {
        return res.get(index++);
    }

    /** @return whether we have a next smallest number */
    public boolean hasNext() {
        return index < res.size();
    }
}
